function returnHouse() {
	var url = "/IMS/house/showList.ht";
	url = url + "?cid=" + cid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

$(function() {
	areaInfo();
	
	if (isFlag != "1") {
		banInfo();
	}
	if ($("#houseType").val() == "02") {
		$("#belongBuildingFloorTr").hide();
		$("#houseNumRequire").children().prev().remove();
		setValidate($("#houBaseForm"), {
			rules : {
				houseBuildArea : {
					required : true,
					maxlength : 15,
					isMoney : true
				},
				belongBuildingNum : {
					required : true,
					maxlength : 10
				},
				belongBuildingUnit : {
					maxlength : 3,
					isUnit : true
				},
				belongBuildingFloor : {
					maxlength : 3,
					nonZeroInteger : true
				},
				houseNum : {
					maxlength : 10,
					isHouseNum : true
				},
				cid : {
					required : true
				},
				houseUsedStatus : {
					required : true
				},
				comment : {
					maxlength : 50
				}
			},
			messages : {
				houseBuildArea : {
					required : "请输入房屋面积",
					maxlength : "面积不能超过15位"
				},
				belongBuildingNum : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				},
				belongBuildingUnit : {
					maxlength : "所属单元不能超过3位数字"
				},
				belongBuildingFloor : {
					maxlength : "所属楼层不能超过3位数字"
				},
				houseNum : {
					maxlength : "门牌号不能超过10位数字"
				},
				cid : {
					required : "请选择小区"
				},
				houseUsedStatus : {
					required : "请选择房屋使用状态"
				},
				comment : {
					maxlength : "备注不能超过50位"
				}
			}
		});
	} else if ($("#houseType").val() == "01") {
		setValidate($("#houBaseForm"), {
			rules : {
				houseBuildArea : {
					required : true,
					maxlength : 15,
					isMoney : true
				},
				belongBuildingNum : {
					required : true,
					maxlength : 10
				},
				belongBuildingUnit : {
					maxlength : 3,
					isUnit : true
				},
				belongBuildingFloor : {
					required : true,
					maxlength : 3,
					nonZeroInteger : true
				},
				houseNum : {
					required : true,
					maxlength : 10,
					isHouseNum : true
				},
				cid : {
					required : true
				},
				houseUsedStatus : {
					required : true
				},
				comment : {
					maxlength : 50
				}
			},
			messages : {
				houseBuildArea : {
					required : "请输入房屋面积",
					maxlength : "面积不能超过15位"
				},
				belongBuildingNum : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				},
				belongBuildingUnit : {
					maxlength : "所属单元不能超过3位数字"
				},
				belongBuildingFloor : {
					required : "请输入所属楼层",
					maxlength : "所属楼层不能超过3位数字"
				},
				houseNum : {
					required : "请输入门牌号",
					maxlength : "门牌号不能超过10位数字"
				},
				cid : {
					required : "请选择小区"
				},
				houseUsedStatus : {
					required : "请选择房屋使用状态"
				},
				comment : {
					maxlength : "备注不能超过50位"
				}
			}
		});
	} else if ($("#houseType").val() == "04") {
		$("#belongBuildingFloorRequire").children().prev().remove();
		$("#houseNumRequire").children().prev().remove();
		$("#commentRequire").prepend("<span class=\"require red\">*</span>");
		setValidate($("#houBaseForm"), {
			rules : {
				houseBuildArea : {
					maxlength : 15,
					isMoney : true
				},
				belongBuildingNum : {
					required : true,
					maxlength : 10
				},
				belongBuildingUnit : {
					maxlength : 3,
					isUnit : true
				},
				belongBuildingFloor : {
					maxlength : 3,
					nonZeroInteger : true
				},
				houseNum : {
					maxlength : 10,
					isHouseNum : true
				},
				cid : {
					required : true
				},
				houseUsedStatus : {
					required : true
				},
				comment : {
					required : true,
					maxlength : 50
				}
			},
			messages : {
				houseBuildArea : {
					maxlength : "面积不能超过15位"
				},
				belongBuildingNum : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				},
				belongBuildingUnit : {
					maxlength : "所属单元不能超过3位数字"
				},
				belongBuildingFloor : {
					maxlength : "所属楼层不能超过3位数字"
				},
				houseNum : {
					maxlength : "门牌号不能超过10位数字"
				},
				cid : {
					required : "请选择小区"
				},
				houseUsedStatus : {
					required : "请选择房屋使用状态"
				},
				comment : {
					required : "请输入备注",
					maxlength : "备注不能超过50位"
				}
			}
		});
	} else if ($("#houseType").val() == "03") {
		$("#belongBuildingFloorRequire").children().prev().remove();
		setValidate($("#houBaseForm"), {
			rules : {
				houseBuildArea : {
					maxlength : 15,
					isMoney : true
				},
				belongBuildingNum : {
					required : true,
					maxlength : 10
				},
				belongBuildingUnit : {
					maxlength : 3,
					isUnit : true
				},
				belongBuildingFloor : {
					maxlength : 3,
					nonZeroInteger : true
				},
				houseNum : {
					required : true,
					maxlength : 10,
					isShopNum : true
				},
				cid : {
					required : true
				},
				houseUsedStatus : {
					required : true
				},
				comment : {
					maxlength : 50
				}
			},
			messages : {
				houseBuildArea : {
					maxlength : "面积不能超过15位"
				},
				belongBuildingNum : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				},
				belongBuildingUnit : {
					maxlength : "所属单元不能超过3位数字"
				},
				belongBuildingFloor : {
					maxlength : "所属楼层不能超过3位数字"
				},
				houseNum : {
					required : "请输入门牌号",
					maxlength : "门牌号不能超过10位数字"
				},
				cid : {
					required : "请选择小区"
				},
				houseUsedStatus : {
					required : "请选择房屋使用状态"
				},
				comment : {
					maxlength : "备注不能超过50位"
				}
			}
		});
	}

	$(".ks-switchable-nav a").click(function() {
		if (curHid || $(this).attr("id") == "base_a") {
			return true;
		} else {
			_alert("请填写基础信息并保存后再填写其他信息");
			return false;
		}
	});

	$("#houseType").change(function() {
		if ($("#houseType").val() == "02") {
			$("#belongBuildingNumRequire").children().prev().remove();
			$("#belongBuildingNumRequire").prepend("<span class=\"require red\">*</span>");
			$("#commentRequire").children().prev().remove();
			$("#belongBuildingFloorTr").hide();
			$("#belongBuildingFloor").val("");
			$("#houseNumRequire").children().prev().remove();
			$("#houseNum").rules("remove");
			$("#houseNum").rules("add", {
				maxlength : 10,
				isHouseNum : true,
				messages : {
					maxlength : "门牌号不能超过10位数字"
				}
			});
			$("#belongBuildingFloor").rules("remove");
			$("#belongBuildingFloor").rules("add", {
				maxlength : 3,
				nonZeroInteger : true,
				messages : {
					maxlength : "所属楼层不能超过3位数字"
				}
			});
			$("#belongBuildingNum").rules("remove");
			$("#belongBuildingNum").rules("add", {
				required : true,
				maxlength : 10,
				messages : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				}
			});
			$("#comment").rules("remove");
			$("#comment").rules("add", {
				maxlength : 50,
				messages : {
					maxlength : "备注不能超过50位"
				}
			});
		} else if ($("#houseType").val() == "01") {
			$("#belongBuildingFloorTr").show();
			$("#belongBuildingNumRequire").children().prev().remove();
			$("#belongBuildingNumRequire").prepend("<span class=\"require red\">*</span>");
			$("#belongBuildingFloorRequire").children().prev().remove();
			$("#belongBuildingFloorRequire").prepend("<span class=\"require red\">*</span>");
			$("#houseNumRequire").children().prev().remove();
			$("#houseNumRequire").prepend("<span class=\"require red\">*</span>");
			$("#commentRequire").children().prev().remove();
			$("#houseNum").rules("remove");
			$("#houseNum").rules("add", {
				required : true,
				maxlength : 10,
				isHouseNum : true,
				messages : {
					required : "请输入门牌号",
					maxlength : "门牌号不能超过10位数字"
				}
			});
			$("#belongBuildingFloor").rules("remove");
			$("#belongBuildingFloor").rules("add", {
				required : true,
				maxlength : 3,
				nonZeroInteger : true,
				messages : {
					required : "请输入所属楼层",
					maxlength : "所属楼层不能超过3位数字"
				}
			});
			$("#belongBuildingNum").rules("remove");
			$("#belongBuildingNum").rules("add", {
				required : true,
				maxlength : 10,
				messages : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				}
			});
			$("#comment").rules("remove");
			$("#comment").rules("add", {
				maxlength : 50,
				messages : {
					maxlength : "备注不能超过50位"
				}
			});
		} else if ($("#houseType").val() == "03") {
			$("#belongBuildingFloorTr").show();
			$("#belongBuildingNumRequire").children().prev().remove();
			$("#belongBuildingNumRequire").prepend("<span class=\"require red\">*</span>");
			$("#belongBuildingFloorRequire").children().prev().remove();
			$("#houseNumRequire").children().prev().remove();
			$("#houseNumRequire").prepend("<span class=\"require red\">*</span>");
			$("#commentRequire").children().prev().remove();
			$("#houseNum").rules("remove");
			$("#houseNum").rules("add", {
				required : true,
				maxlength : 10,
				isShopNum : true,
				messages : {
					required : "请输入门牌号",
					maxlength : "门牌号不能超过10位数字"
				}
			});
			$("#belongBuildingNum").rules("remove");
			$("#belongBuildingNum").rules("add", {
				maxlength : 10,
				messages : {
					maxlength : "楼栋号不能超过10个字符"
				}
			});
			$("#belongBuildingFloor").rules("remove");
			$("#belongBuildingFloor").rules("add", {
				maxlength : 3,
				nonZeroInteger : true,
				messages : {
					maxlength : "所属楼层不能超过3位数字"
				}
			});
			$("#belongBuildingNum").rules("remove");
			$("#belongBuildingNum").rules("add", {
				required : true,
				maxlength : 10,
				messages : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				}
			});
			$("#comment").rules("remove");
			$("#comment").rules("add", {
				maxlength : 50,
				messages : {
					maxlength : "备注不能超过50位"
				}
			});
		} else if ($("#houseType").val() == "04") {
			$("#belongBuildingNumRequire").children().prev().remove();
			$("#belongBuildingNumRequire").prepend("<span class=\"require red\">*</span>");
			$("#belongBuildingFloorTr").show();
			$("#belongBuildingFloorRequire").children().prev().remove();
			$("#houseNumRequire").children().prev().remove();
			$("#commentRequire").children().prev().remove();
			$("#commentRequire").prepend("<span class=\"require red\">*</span>");
			$("#houseNum").rules("remove");
			$("#houseNum").rules("add", {
				maxlength : 10,
				isHouseNum : true,
				messages : {
					maxlength : "门牌号不能超过10位数字"
				}
			});

			$("#comment").rules("remove");
			$("#comment").rules("add", {
				required : true,
				maxlength : 50,
				messages : {
					required : "请输入备注",
					maxlength : "备注不能超过50位"
				}
			});

			$("#belongBuildingFloor").rules("remove");
			$("#belongBuildingFloor").rules("add", {
				maxlength : 3,
				nonZeroInteger : true,
				messages : {
					maxlength : "所属楼层不能超过3位数字"
				}
			});
			$("#belongBuildingNum").rules("remove");
			$("#belongBuildingNum").rules("add", {
				required : true,
				maxlength : 10,
				messages : {
					required : "请输入所属楼栋",
					maxlength : "楼栋号不能超过10个字符"
				}
			});
		}
	});
	$("#devSelect2 option[value='" + houseDid + "']").attr("selected", true);
	$("#hcSelect2  option[value='" + houseCategory + "']").attr("selected", true);
	$("#htSelect2  option[value='" + houseHtid + "']").attr("selected", true);
	$("#devSelect2").select2({
		placeholder : "请选择所属开发商"
	});
	$("#hcSelect2").select2({
		placeholder : "请选择房屋用途"
	});
	$("#houseUsedStatus").select2({
		placeholder : "请选择房屋使用状态"
	});

	searchHouseType(houseHtid);
});

function searchHouseType(houseHtid) {
	$.ajax({
		url : "/IMS/house/searchHouseTypes.ht",
		data : {
			cid : cid
		},
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data) {
			if (data.code == 0) {
				$("#htSelect2").children().remove();
				if (data.result && data.result.length > 0) {
					$("#htSelect2").append("<option></option>");
					$.each(data.result, function(index) {
						//console.log("houseHtid:" + houseHtid);
						//console.log("htid" + this.htid);
						if (houseHtid == this.htid) {
							var dom = "<option value='" + this.htid + "' selected='selected'>" + this.houseTypeName + "</option>";
						} else {
							var dom = "<option value='" + this.htid + "'>" + this.houseTypeName + "</option>";
						}
						$("#htSelect2").append(dom);
					});
					$("#htSelect2").select2({
						placeholder : "请选择户型"
					});
				} else {
					$("#htSelect2").select2({
						placeholder : "该小区尚未录入户型信息"
					});
				}
				hideProccessBar();
			} else {
				hideProccessBar();
			}
		}
	});
}

function checkHouse() {
	// 先验证必输项，再验证房屋重复
	if (!$("#houBaseForm").valid()) {
		return;
	}
	
	if ($("#houseType").val() == "04") {
		// 如果房屋类型是其他，不做重复判断
		var areaVal = $("#houseBuildArea").val();
		if (parseFloat(areaVal) > 1000) {
			var msg = '房屋面积大于1000,请检查 ';
			_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
				$.submit($('#houBaseForm'), {
					dataType : 'json',
					beforeSend : function() {
						showProccessBar();
					},
					error : function() {
						hideProccessBar();
					},
					success : function(data, textStatus) {
						returnHouse();
					}
				});
			});
		} else {
			$.submit($('#houBaseForm'), {
				dataType : 'json',
				beforeSend : function() {
					showProccessBar();
				},
				error : function() {
					hideProccessBar();
				},
				success : function(data, textStatus) {
					returnHouse();
				}
			});
		}
	} else {
		$.ajax({
			type : "POST",
			url : "/IMS/house/isExsit.ht",
			data : {
				"belongBuildingArea" : $("#belongBuildingArea").val(),
				"hid" : curHid,
				"belongBuildingNum" : $("#belongBuildingNum").val(),
				"belongBuildingFloor" : $("#belongBuildingFloor").val(),
				"belongBuildingUnit" : $("#belongBuildingUnit").val(),
				"houseNum" : $("#houseNum").val(),
				"houseType" : $("#houseType").val(),
				"cid" : cid
			},
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data) {
				if (!data) {
					if ($("#houseType").val() == '01') {
						_alert("已录入此房屋或存在相同区域相同楼栋的别墅");
					} else if ($("#houseType").val() == '02') {
						_alert("已录入此别墅或存在相同区域相同楼栋的普通住房和商铺");
					} else if ($("#houseType").val() == '03') {
						_alert("已录入此房屋或存在相同区域相同楼栋的别墅");
					}
					hideProccessBar();
				} else {
					var areaVal = $("#houseBuildArea").val();
					if (parseFloat(areaVal) > 1000) {
						var msg = '房屋面积大于1000,请检查 ';
						_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
							$.submit($('#houBaseForm'), {
								dataType : 'json',
								beforeSend : function() {
									showProccessBar();
								},
								error : function() {
									hideProccessBar();
								},
								success : function(data, textStatus) {
									returnHouse();
								}
							});
						});
					} else {
						$.submit($('#houBaseForm'), {
							dataType : 'json',
							beforeSend : function() {
								showProccessBar();
							},
							error : function() {
								hideProccessBar();
							},
							success : function(data, textStatus) {
								returnHouse();
							}
						});
					}
				}
			}
		});
	}
}

function changeSelect() {
	areaInfo();
}

function changeArea() {
	banInfo();
}

function changeUnit() {
	houUnitInfo();
}

function areaInfo() { // 房屋区域信息
	var cid = $('#communityId').val();
	var _url = "/IMS/house/initHouAreaInfo.ht";
	$.ajax({
		url : _url,
		data : {
			"cid" : cid,
		},
		type : 'post',
		success : function(data) {
			initArea(data.result);
		}
	});
}

function initArea(data) { // 初始化房屋区域信息
	$("#controlIds").attr("class", "control-css3select item-box-p1 css3select-icon-hide");
	if (data != null && data.length > 0) {
		$("#belongBuildingArea").children().remove();
		var optionConten = "<option value='noArea'>其他</option>";
		if (houseBelongArea == "" || houseBelongArea == null) {
			optionConten = "";
		}
		$.each(data, function(index) {
			if (this.area == "" && houseBelongArea == "") {
				optionConten += "<option selected='selected' value='noArea'>其他</option>";
			} else if (this.area == houseBelongArea) {
				optionConten += "<option selected='selected' value='" + this.area + "'>" + this.area + "</option>";
			} else {
				optionConten += "<option value='" + this.area + "'>" + this.area + "</option>";
			}
		});
		$("#belongBuildingArea").append(optionConten);
		$("#belongBuildingArea").select2({
			placeholder : "请选择"
		});
		// 新建时触发
		if ((houseBelongArea == "" || houseBelongArea == null) && isFlag != "1") {
			changeArea();
		}
	} else {
		$("#belongBuildingArea").select2({
			placeholder : "没有相应区域信息",
		});
	}
}

function banInfo() { // 房屋楼栋信息
	var cid = $('#communityId').val();
	var area = $('#belongBuildingArea').val();
	var _url = "/IMS/house/initHouBanInfo.ht";
	$.ajax({
		url : _url,
		data : {
			"cid" : cid,
			"area" : area,
		},
		type : 'post',
		success : function(data) {
			initBan(data.result);
		}
	});
}

function initBan(data) { // 初始化房屋楼栋信息
	$("#controlId").attr("class", "control-css3select item-box-p1 css3select-icon-hide");
	$("#belongBuildingNum").children().remove();
	if (data.length > 0) {
		$("#belongBuildingNum").append("<option></option>");
		$.each(data, function(index) {
			var dom = "";
			dom = "<option value='" + this.ban + "'>" + this.ban + "</option>";
			$("#belongBuildingNum").append(dom);
		});
		$("#belongBuildingNum").select2({
			placeholder : "请选择"
		});
	} else {
		$("#belongBuildingNum").select2({
			placeholder : "没有相应楼栋信息",
		});
	}
}

function houUnitInfo() { // 房屋单元信息
	var cid = $('#communityId').val();
	var area = $('#belongBuildingArea').val(); // 区域
	var ban = $('#belongBuildingNum').val(); // 楼栋
	if (Is_Null(cid) || Is_Null(ban)) {
		_alert("小区和楼栋不能为空");
	} else {
		var _url = "/IMS/house/initHouseUnitInfo.ht";
		$.ajax({
			url : _url,
			data : {
				"cid" : cid,
				"ban" : ban,
				"area" : area,
			},
			type : 'post',
			success : function(data) {
				initUnit(data.result);
			}
		});
	}
}

function initUnit(data) { // 初始化房屋单元信息
	$("#controlIdss").attr("class", "control-css3select item-box-p1 css3select-icon-hide");
	$("#belongBuildingUnit").children().remove();
	if (data != null && data.length > 0) {
		$("#belongBuildingUnit").append("<option></option>");
		$.each(data, function(index) {
			var dom = "";
			dom = "<option value='" + data[index] + "'>" + data[index] + "</option>";
			$("#belongBuildingUnit").append(dom);
		});
		$("#belongBuildingUnit").select2({
			placeholder : "请选择"
		});
	} else {
		$("#belongBuildingUnit").select2({
			data : [ {
				id : '',
				text : '其他',
				value : '',
				selected : 'selected'
			} ]
		});
	}
}